package training.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

class CustomListener implements ActionListener {

	JLabel label;

	CustomListener(JLabel label) {
		this.label = label;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		label.setText("You clicked " + e.getActionCommand());

	}
}

public class SwingsDemo extends JFrame {
	public SwingsDemo() {
		setSize(300, 300);
		setTitle("Hello World");
		JButton left = new JButton("Left");
		Button right = new Button("right");
		JTextArea center = new JTextArea("sample text area");
		JLabel top = new JLabel("Swings demo");
		final JLabel bottom = new JLabel("Bottom of swing pane");

		// getContentPane().setLayout(new FlowLayout());
		/*
		 * getContentPane().setLayout(new BoxLayout(this, 1));
		 * getContentPane().add(left); getContentPane().add(right);
		 * getContentPane().add(center); getContentPane().add(top);
		 * getContentPane().add(bottom);
		 */

		getContentPane().add(left, BorderLayout.WEST);
		getContentPane().add(right, BorderLayout.EAST);
		getContentPane().add(center, BorderLayout.CENTER);
		getContentPane().add(top, BorderLayout.NORTH);
		getContentPane().add(bottom, BorderLayout.SOUTH);

		left.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				bottom.setText("You clicked " + e.getActionCommand());

			}
		});
		CustomListener customListener = new CustomListener(bottom);
		right.addActionListener(customListener);
        center.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				bottom.setText("mouseReleased ");
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				bottom.setText("mousePressed ");
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				bottom.setText("mouseExited ");
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				bottom.setText("mouseEntered ");
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				bottom.setText("mouseClicked ");
				
			}
		});
		center.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {

				bottom.setText("keyTyped " + e.getKeyChar());
			}

			@Override
			public void keyReleased(KeyEvent e) {
				bottom.setText("keyReleased " + e.getKeyChar());

			}

			@Override
			public void keyPressed(KeyEvent e) {
				bottom.setText("keyPressed " + e.getKeyChar());

			}
		});

		setVisible(true);
	}

	public static void main(String[] args) {
		SwingsDemo myHello = new SwingsDemo();
	}

}